// packageF/balance/balance.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs:[
      {
        name:'',
        status:0,
        orderList:[],
        type:'dividend_alias'
      },
      {
        name:'',
        status:1,
        type:'flat_prize',
        orderList:[]
      },
      {
        name:'',
        type:'extra_award',
        status:3,
        orderList:[]
      }
    ],
    customName:{},
    orderData:[],
    isLoadMore: true,
    page:1,
    totalPage:0,
    tabIndex:0,
    distinction:'balance', //区别组件有不同的数据渲染
    starTime:null, //开始时间
    endTime:null, //结算时间
    diviType:0 , // 默认状态
    finish:0, //已结算收益
    wait:0, //已结算收益
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if((options.start_time ?? '') !== '' || (options.end_time ?? '') !== '') {
      this.setData({
        starTime:options.start_time,
        endTime:options.end_time
      })
    }
    this.balanceData();
  },
     /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading();
     //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
     wx.showLoading({
      title: '刷新中...',
    })
    this.setData({
      page:1
    })
    this.balanceData();
  },
  balanceData() {
    let urlStr = app.getNetAddresss("plugin.team-analysis.frontend.detail.incomeDetail");
    let json = {
      start_time:this.data.starTime,
      end_time:this.data.endTime,
      page: this.data.page,
      divi_type:this.data.diviType
    };
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        let list = res.data.data;
        console.log(res);
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          this.data.isLoadMore = true;
          this.data.totalPage = res.data.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          let customName = res.data.custom_name;
          let tabsList = this.data.tabs;
          Object.keys(customName).forEach((item,index) => { //遍历后端给的自定义字段到tab数组里面
            for(let i in tabsList) {
              if(tabsList[i].type === item) {
                tabsList[i].name = customName[item];
              }
            }
          })
          this.setData({
            orderData : list,
            finish:res.data.finish,
            wait:res.data.wait,
            tabs: tabsList
          })
        } else {
          this.data.page = this.data.page - 1;
          this.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    const that = this;
    that.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let urlStr = app.getNetAddresss("plugin.team-analysis.frontend.detail.incomeDetail");
      let json = {
        start_time:this.data.starTime,
        end_time:this.data.endTime,
        page: this.data.page,
        divi_type:this.data.diviType
      };
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.data;
          console.log(res);
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              orderData : this.data.orderData.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  onChange(event) {
    this.setData({
      diviType:event.detail.name,
      page:1
    })
    this.balanceData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})